package top.kaoshanji.ch02;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 代码清单 ch02-01：DirectoryStream示例(2.3.1节、在目录中查找文件)
 * API文档有示例
 * author kaoshanji
 * ^_^
 * create time 2024/12/13 15:47
 */
public class DirectoryStreamDemo {

    private static final Logger logger = LogManager.getLogger(DirectoryStreamDemo.class);

    public static void main(String[] args) {
        logger.info("...代码清单 ch02-01：DirectoryStream示例(2.3.1节、在目录中查找文件)...");

        // 设定起始路径
        Path dir = Paths.get("D:\\works\\workspace\\java7developer");

        // 声明过滤流
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.properties")) {
            // 找出所有 .properties 文件并输出
            for (Path entry: stream) {
                System.out.println(entry.getFileName());
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

    }

}
